Understand CodeBehind

Although our first example was okay, we unfortunately broke one of the ASP.NET coding principles: To separate markup and code as you saw, we added a scripting block (using <%%>) , Where we wrote a line of C # code to use the label, though it is okay for a small and simple example, but we will soon get a real disturbance with a bunch of C # code. Can appropriate places, which is a large amount of HTML code if you also throw some JavaScript and CSS, it will soon be too disorganized to be edited. This is why MS has provided codebahind, a technique that allows you to completely separate markup (HTML, CSS etc.) and code (C #, VBnet etc). So let's delete the script block (<% to%>) and save the file.

As we talked about earlier, V. S. Said a file as default .aspx.cs. If you can not see it in Solution Explorer, click on the left plus sign of the default .aspx file. Open this file Now, if you have not previously worked with .NET or other non-web programming languages, then at this point it might seem a little scary. It does not look like HTML, however, I will try to explain the various parts of the material, and soon you hope that Codex is a great tool for a better view of your work. Here is a complete list of files as it looks like:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

For now, the top portion is irrelevant to us. This is a list of the names of the keywords to be used in the file. Since this is an ASP.NET tutorial and not a dedicated C # tutorial, I will not understand it in depth. After this, we have class. Classes are a part of the concept of object-oriented programming, which has become very popular, especially with languages ​​like Java and C. OO is a very complex subject, which will not be understood in this tutorial.

The name of this class is "_Default", and: (colon) tells us that this class system Receives from page class in page. Web. UI namespace. This means that our pages can already do a bunch of things without any programming, because it attains the other class's methods and properties. All ASP.NET pages handle page pages, or other classes that are derived from the page class

The only way in this category is Page_load, which is loaded every time the page is loaded. Let us use it for our benefit, and by this method set ext. We can use the exact line of code as before, but definitely without the script block tag. Add lines of code between {and} characters:


HelloWorldLabel.Text = "Hello, World!";

Bus. Run the project (F6), and take a look. Page looks like a first, but we just used CodeBehind for the first time. But this example is getting old, so in the next chapter, we will see something interesting